<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\db;
/**
 * 图片库
 *
 * @icon fa fa-circle-o
 */
class BydPricture extends Backend
{

    /**
     * BydPricture模型对象
     * @var \app\admin\model\BydPricture
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $type=['1'=>'轮播图','2'=>'详情图','3'=>'封面图','4'=>'VR图片','5'=>'视频','6'=>'设计师图片','7'=>'设计师灵感'];
        $link=['0'=>'产品','1'=>'套系'];
        $jump=['0'=>'不跳转','1'=>'跳转'];
        $this->view->assign("jump", $jump);
        $this->view->assign("link", $link);
        $this->view->assign("type", $type);
        $this->model = new \app\admin\model\BydPricture;

    }

    public function import()
    {
        parent::import();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                    ->with(['bydshop'])
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);

            foreach ($list as $row) {


            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }
    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {

                if($params['link']==0){
                    $where['id']=$params['shopid'];
                    //添加商品封面图
                    $find=\app\admin\model\BydShop::find($where['id']);
                    if($params['type']==1){
                        //轮播图
                        if(empty($find['image'])){
                            //第一次添加轮播图
                            $edit=\app\admin\model\BydShop::where('id',$where['id'])->update(['image'=>$params['image']]);
                        }else{
                            $arr = explode(",", $find['image']);
                            $image1=$find['image'];
                            $image2=$params['image'];
                            $image="$image1,$image2";
                            $edit=\app\admin\model\BydShop::where('id',$where['id'])->update(['image'=>$image]);
                        }
                    }
                    if($params['type']==3){

                        //封面图
                        if (!empty($find)){
                            $edit=\app\admin\model\BydShop::where('id',$where['id'])->update(['simage'=>$params['image']]);
                        }
                    }elseif ($params['type']==2){

                        //详情图
                        if(empty($find['content'])){
                            //第一次添加详情
                            $edit=\app\admin\model\BydShop::where('id',$where['id'])->update(['content'=>$params['image']]);
                        }else{
                            $arr = explode(",", $find['content']);
                            $image1=$find['content'];
                            $image2=$params['image'];
                            $image="$image1,$image2";
                            $edit=\app\admin\model\BydShop::where('id',$where['id'])->update(['content'=>$image]);
                        }
                    }
                }else{
                    $where['id']=$params['txid'];
                    $find=\app\admin\model\BydSet::find($where['id']);
                    if($params['type']==1){
                        //轮播图
                        if(empty($find['image'])){
                            //第一次添加轮播图
                            $edit=\app\admin\model\BydSet::where('id',$where['id'])->update(['image'=>$params['image']]);
                        }else{
                            $arr = explode(",", $find['image']);
                            $image1=$find['image'];
                            $image2=$params['image'];
                            $image="$image1,$image2";
                            $edit=\app\admin\model\BydSet::where('id',$where['id'])->update(['image'=>$image]);
                        }
                    }elseif($params['type']==3){
                        //封面图
                        if (!empty($find)){
                            $edit=\app\admin\model\BydSet::where('id',$where['id'])->update(['simage'=>$params['image']]);
                        }
                    }elseif ($params['type']==4){
                        //vr封面图
                        $edit=\app\admin\model\BydSet::where('id',$where['id'])->update(['vrimage'=>$params['image']]);
                        if(!empty($params['vrurl'])){
                            $vredit=\app\admin\model\BydSet::where('id',$where['id'])->update(['url'=>$params['vrurl']]);
                        }
                        $edit=\app\admin\model\BydSet::where('id',$where['id'])->update(['vrimage'=>$params['image']]);
                    }elseif ($params['type']==5){
                        //套系视频
                        $edit=\app\admin\model\BydSet::where('id',$where['id'])->update(['video'=>$params['video']]);
                    }elseif ($params['type']==6){
                        //设计师图片
                        $edit=\app\admin\model\BydSet::where('id',$where['id'])->update(['designerimage'=>$params['image']]);
                    }
                }


                $params = $this->preExcludeFields($params);

                if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
                    $params[$this->dataLimitField] = $this->auth->id;

                }
                $result = false;
                Db::startTrans();
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
                        $qq=$this->model->validateFailException(true)->validate($validate);
                    }

                    $result = $this->model->allowField(true)->save($params);

                    Db::commit();
                } catch (ValidateException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (PDOException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    $this->success();
                } else {
                    $this->error(__('No rows were inserted'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        return $this->view->fetch();
    }
    public function geiid($sequence= null){
        return $this->connection->getLastInsID($sequence);
    }
    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }

        $setxq=\app\admin\model\BydSetxq::where('imageid',$row['image'])->find();

        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                if($setxq){
                    $update=\app\admin\model\BydSetxq::where('id',$setxq['id'])->update(['imageid'=>$params['image']]);
                }
                $params = $this->preExcludeFields($params);
                $result = false;
                Db::startTrans();
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
                        $row->validateFailException(true)->validate($validate);
                    }
                    $result = $row->allowField(true)->save($params);
                    Db::commit();
                } catch (ValidateException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (PDOException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {


                    if($row['link']==0){
                        $where['shopid']=$row['shopid'];
                        $where['type']=$row['type'];
                        $shop=\app\admin\model\BydPricture::where($where)->select();
                        if(!empty($shop)){
                            foreach ($shop as $v){
                                $arr[]=$v['image'];
                            }
                            $image = implode(",", $arr);
                            if($row['type']==1){
                                $edit=\app\admin\model\BydShop::where('id',$row['shopid'])->update(['image'=>$image]);
                            }elseif ($row['type']==2){
                                $edit=\app\admin\model\BydShop::where('id',$row['shopid'])->update(['content'=>$image]);
                            }elseif($row['type']==3){
                                $edit=\app\admin\model\BydShop::where('id',$row['shopid'])->update(['simage'=>$image]);
                            }
                        }
                    }else{

                        $where['txid']=$row['txid'];
                        $where['type']=$row['type'];
                        $shop=\app\admin\model\BydPricture::where($where)->select();

                        if(!empty($shop)){
                            foreach ($shop as $v){
                                $arr[]=$v['image'];

                            }
                            $image = implode(",", $arr);
                            if($row['type']==1){
                                $edit=\app\admin\model\BydSet::where('id',$row['txid'])->update(['image'=>$image]);
                            }elseif($row['type']==3){
                                $edit=\app\admin\model\BydSet::where('id',$row['txid'])->update(['simage'=>$image]);
                            }elseif($row['type']==4){
                                $edit=\app\admin\model\BydSet::where('id',$row['txid'])->update(['vrimage'=>$image]);
                                if(!empty($params['vrurl'])){
                                    $vredit=\app\admin\model\BydSet::where('id',$row['txid'])->update(['url'=>$params['vrurl']]);
                                }
                            }elseif($row['type']==5){
                                foreach ($shop as $v){
                                    $arr[]=$v['video'];
                                }
                                $video = implode(",", $arr);
                                $edit=\app\admin\model\BydSet::where('id',$row['txid'])->update(['video'=>$video]);
                            }elseif($row['type']==6){
                                $edit=\app\admin\model\BydSet::where('id',$row['txid'])->update(['designerimage'=>$image]);
                            }
                        }
                    }

                    $this->success();
                } else {
                    $this->error(__('No rows were updated'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }
    /**
     * 删除
     */
    public function del($ids = "")
    {
        if (!$this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $ids = $ids ? $ids : $this->request->post("ids");
        if ($ids) {
            $row=\app\admin\model\BydPricture::where('id',$ids)->find();
            $pk = $this->model->getPk();
            $adminIds = $this->getDataLimitAdminIds();
            if (is_array($adminIds)) {
                $this->model->where($this->dataLimitField, 'in', $adminIds);
            }
            $list = $this->model->where($pk, 'in', $ids)->select();

            $count = 0;
            Db::startTrans();
            try {
                foreach ($list as $k => $v) {
                    $count += $v->delete();
                }
                Db::commit();
            } catch (PDOException $e) {
                Db::rollback();
                $this->error($e->getMessage());
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if ($count) {
                if($row['link']==0){
                    $where['shopid']=$row['shopid'];
                    $where['type']=$row['type'];
                    $shop=\app\admin\model\BydPricture::where($where)->select();
                    if(!empty($shop)){
                        foreach ($shop as $v){
                            $arr[]=$v['image'];
                        }
                        $image = implode(",", $arr);
                    } else{
                        $image = null;
                    }
                    if($row['type']==1){
                        $edit=\app\admin\model\BydShop::where('id',$row['shopid'])->update(['image'=>$image]);
                    }elseif ($row['type']==2){
                        $edit=\app\admin\model\BydShop::where('id',$row['shopid'])->update(['content'=>$image]);
                    }elseif($row['type']==3){
                        $edit=\app\admin\model\BydShop::where('id',$row['shopid'])->update(['simage'=>$image]);
                    }

                }else{
                    $where['txid']=$row['txid'];
                    $where['type']=$row['type'];
                    $shop=\app\admin\model\BydPricture::where($where)->select();

                    if(!empty($shop)){
                        foreach ($shop as $v){
                            $arr[]=$v['image'];
                        }
                    }else{
                        $image = null;
                    }
                    $image = implode(",", $arr);
                    if($row['type']==1){
                        $edit=\app\admin\model\BydSet::where('id',$row['txid'])->update(['image'=>$image]);
                    }elseif($row['type']==3){
                        $edit=\app\admin\model\BydSet::where('id',$row['txid'])->update(['simage'=>$image]);
                    }elseif($row['type']==4){
                        $edit=\app\admin\model\BydSet::where('id',$row['txid'])->update(['vrimage'=>$image]);
                    }elseif($row['type']==5){
                        foreach ($shop as $v){
                            $arr[]=$v['video'];
                        }
                        $video = implode(",", $arr);
                        $edit=\app\admin\model\BydSet::where('id',$row['txid'])->update(['video'=>$video]);
                    }elseif($row['type']==6){
                        $edit=\app\admin\model\BydSet::where('id',$row['txid'])->update(['designerimage'=>$image]);
                    }

                }

                $this->success();
            } else {
                $this->error(__('No rows were deleted'));
            }
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }
    public function select()
    {
        if ($this->request->isAjax()) {

            return $this->index();
        }
        return $this->view->fetch();
    }
}
